package homework;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class homework8 {
    public static void main(String[] args) {
        System.out.println("随机点名器");
        //创建一个存储多个同学名字的容器
        ArrayList<String> students = addStudent();
        printStudent(students);
        randonStudentName(students);
    }
    public static ArrayList<String> addStudent(){
        ArrayList<String> name = new ArrayList<String>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入同学的姓名（输入 q 结束输入）：");
        String students = scanner.next();
        while (!students.equals("q")) {
            name.add(students);
            students = scanner.next();
        }
        scanner.close();
        return name;
    }
    private static void randonStudentName(ArrayList<String> names) {
        Random rand = new Random();
        int randomIndex = rand.nextInt(names.size());
        String randomName = names.get(randomIndex);
        System.out.println("幸运儿：" + randomName);
    }
    private static void printStudent(ArrayList<String> names) {
        System.out.println("全班同学的姓名：");
        for (String n : names) {
            System.out.println(n);
        }
    }
}
